﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;


namespace Elementum
{
    public class StatusBar : Image
    {
        const int Width = 800;
        const int Height = 36;
        protected List<Animation> _bar = new List<Animation>();
        protected List<Color> _color = new List<Color>();
        int i = 0;
        SpriteFont Font;
        Texture2D EleTexture;
        public Player Player
        {
            get;
            set;
        }

        public StatusBar(string path, Point point) 
        {
            _texture = ResourceManager.LoadTexture(path);
            _bar.Add(new Animation(_texture, point));
            CurrentAnimation = _bar[0];
            _color.Add(Color.White);
            CurrentColor = _color[0];
            Init();
        }

        public StatusBar(string path)
        {
            _texture = ResourceManager.LoadTexture(path);
            _bar.Add(new Animation(_texture, new Point(Width, Height)));
            CurrentAnimation = _bar[0];
            _color.Add(Color.White);
            CurrentColor = _color[0];
            Init();
        }

        private void Init()
        {
            EleTexture = ResourceManager.LoadTexture("images/items/elementum");
            _texture = ResourceManager.LoadTexture("images/system/blue");
            _bar.Add(new Animation(_texture, new Point(Width, Height)));
            _texture = ResourceManager.LoadTexture("images/system/red");
            _bar.Add(new Animation(_texture, new Point(Width, Height)));
            _texture = ResourceManager.LoadTexture("images/system/green");
            _bar.Add(new Animation(_texture, new Point(Width, Height)));
            _texture = ResourceManager.LoadTexture("images/system/white");
            _bar.Add(new Animation(_texture, new Point(Width, Height)));

            _color.Add(Color.Yellow);
            _color.Add(Color.WhiteSmoke);
            _color.Add(Color.SeaShell);
            _color.Add(Color.Black);

            //this.TouchStateAction += Change;

            Font = ResourceManager.LoadSpriteFont("SpriteFont1");
        }

        
        public Color CurrentColor;
        public override void OnDraw(SpriteBatch spriteBatch)
        {
            //spriteBatch.End();
           // spriteBatch.Begin();
            if (CurrentAnimation != null)
            {
                CurrentAnimation.Alpha = 0.5f;
                CurrentAnimation.OnDraw(spriteBatch, Position);
            }
            //spriteBatch.DrawString(Font, "io:" + StageManager.io, new Vector2(20, 0), CurrentColor);
            //spriteBatch.DrawString(Font, "i:" + StageManager.i, new Vector2(120, 0), CurrentColor);
            spriteBatch.Draw(EleTexture, new Rectangle(1, 1, 40, 35), new Rectangle(1, 1, 40, 35), Color.White, 0, new Vector2(-40, 0), SpriteEffects.None, 1);

            spriteBatch.DrawString(Font, "x" + Player.elementPieceList.Count(), new Vector2(80, 0), CurrentColor);
            //spriteBatch.End();
           // spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, transform);
        }

        bool preIsTouched;

        public void Change(TouchLocationState state)
        {
            if (state == TouchLocationState.Pressed)
            {
                Player.shake = true;
                CurrentAnimation = _bar[i];
                CurrentColor = _color[i];
                i++;
                if (i == 5) i = 0;
            }
        }

        public override void Update(int delta)
        {
            base.Update(delta);
            //if (preIsTouched == true && IsTouched == false)
            //{
            //    Player.shake = true;
            //    CurrentAnimation = _bar[i];
            //    CurrentColor = _color[i];
            //    i++;
            //    if (i == 5) i = 0;
            //}

            CurrentAnimation.LayerDepth = 0.1f;
            preIsTouched = IsTouched;
        }
    }
}
